
package com.jf.cloud.delivery.controller.multishop;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.jf.cloud.api.delivery.vo.SameCityVO;
import com.jf.cloud.common.bean.BmapPoint;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.delivery.dto.SameCityDTO;
import com.jf.cloud.delivery.model.SameCity;
import com.jf.cloud.delivery.service.SameCityService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * @author zz
 * @date 2020-05-23 08:26:25
 */
@RestController("multishopSameCityController")
@RequestMapping("/m/same_city")
@Tag(name = "商家端同城配送接口")
public class SameCityController {

    @Autowired
    private SameCityService sameCityService;

    @GetMapping("/get_same_city_info")
    @Operation(summary = "获取同城配送数据")
    public ServerResponseEntity<SameCityVO> getSameCityPage() {
        Long shopId = AuthUserContext.get().getTenantId();
        SameCity sameCity = sameCityService.getSameCityByShopId(shopId);
        if (sameCity == null) {
            return ServerResponseEntity.success(null);
        }
        SameCityVO sameCityVO = new SameCityVO();
        BeanUtils.copyProperties(sameCity, sameCityVO);
        sameCityVO.setPolygonPath(JSON.parseArray(sameCity.getPositionInfo(), BmapPoint.class));
        return ServerResponseEntity.success(sameCityVO);
    }

    @PostMapping
    @Operation(summary = "新增或修改同城配送数据")
    public ServerResponseEntity<Boolean> saveOrUpdate(@RequestBody @Valid SameCityDTO sameCityDTO) {
        if (CollUtil.isEmpty(sameCityDTO.getPolygonPath())) {
            throw new LuckException("请设置设置配送区域");
        }
        Long shopId = AuthUserContext.get().getTenantId();
        SameCity sameCity = sameCityService.getSameCityByShopId(shopId);
        String positionInfo = JSON.toJSONString(sameCityDTO.getPolygonPath());
        boolean isAdd = false;
        if (sameCity == null) {
            sameCity = new SameCity();
            isAdd = true;
        }
        BeanUtils.copyProperties(sameCityDTO, sameCity);
        sameCity.setPositionInfo(positionInfo);
        sameCity.setShopId(shopId);
        // 新增or更新
        if (isAdd) {
            sameCityService.save(sameCity);
        } else {
            sameCityService.updateByShopId(sameCity);
        }
        // 删除缓存
        sameCityService.removeSameCityCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改同城配送数据")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid SameCityDTO sameCityDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        // 更新同城配送
        SameCity sameCity = new SameCity();
        BeanUtils.copyProperties(sameCityDTO, sameCity);
        sameCity.setShopId(shopId);
        sameCity.setPositionInfo(JSON.toJSONString(sameCityDTO.getPolygonPath()));
        sameCityService.updateByShopId(sameCity);
        // 删除缓存
        sameCityService.removeSameCityCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }
}
